<?php

namespace App\V3\V3_2_7\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\V3_2_7\OperMainAccountRequest;
use App\V3\V3_2_7\Services\OperMainAccountService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class OperMainAccountController extends BaseController
{

    /**
     *主账号使用负数库存
     * Author: zlf
     * Created at 2024/7/10 9:44
     * @param Request $request
     * @return JsonResponse
     */
    public function edit(Request $request): JsonResponse
    {
        if ($this->getMainUserId() !== 1) {
            return fail('', '你无权操作！');
        }
        $data = $request->all();
        $data['time'] = time();
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $this->operateLog('操作主账号使用负数库存', $data, $data['time']);
        return response()->json(OperMainAccountService::edit($data));
    }


    /**
     *删除
     * Author: zlf
     * Created at 2024/7/10 11:28
     * @param Request $request
     * @return JsonResponse
     */
    public function del(Request $request): JsonResponse
    {
        if ($this->getMainUserId() !== 1) {
            return fail('', '你无权操作！');
        }
        if (empty($request->id)) return fail('', '请选择要删除的记录！');
        if (OperMainAccountService::del($request->id)) {
            return $this->success();
        } else {
            return $this->fail('', '删除失败！');
        }
    }

    /**
     *查询主账号使用负数库存信息
     * Author: zlf
     * Created at 2024/7/10 9:59
     * @param OperMainAccountRequest $request
     * @return JsonResponse
     */
    public function info(OperMainAccountRequest $request): JsonResponse
    {
        if ($this->getMainUserId() !== 1) {
            return fail('', '你无权操作！');
        }
        $data = $request->all();
        return $this->success(OperMainAccountService::info($data));
    }
}
